import { Init, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, Equal } from 'typeorm';
import { PushTemplateEntity } from '../entity/template';

/**
 * pushTemplate模块-推送模板信息服务
 */
@Provide()
export class PushTemplateService extends BaseService {
  @InjectEntityModel(PushTemplateEntity)
  pushTemplateEntity: Repository<PushTemplateEntity>;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.pushTemplateEntity);
  }

  /**
   * 根据用户ID查找推送模板
   * @param userId 用户ID
   * @returns 推送模板列表
   */
  async findByUserId(userId: number): Promise<PushTemplateEntity[]> {
    return this.pushTemplateEntity.findBy({ userId: Equal(userId) });
  }

  /**
   * 根据用户ID和模板名称查找推送模板
   * @param userId 用户ID
   * @param name 模板名称
   * @returns 推送模板
   */
  async findOneByUserIdAndName(userId: number, name: string): Promise<PushTemplateEntity> {
    return this.pushTemplateEntity.findOneBy({ userId: Equal(userId), name: Equal(name) });
  }

  /**
   * 根据用户ID和模板ID更新推送模板
   * @param userId 用户ID
   * @param id 模板ID
   * @param updateData 更新数据
   * @returns 更新结果
   */
  async updateByUserIdAndId(userId: number, id: number, updateData: Partial<PushTemplateEntity>): Promise<void> {
    await this.pushTemplateEntity.update({ userId: Equal(userId), id: Equal(id) }, updateData);
  }

  /**
   * 根据用户ID和模板ID删除推送模板
   * @param userId 用户ID
   * @param id 模板ID
   * @returns 删除结果
   */
  async deleteByUserIdAndId(userId: number, id: number): Promise<void> {
    await this.pushTemplateEntity.delete({ userId: Equal(userId), id: Equal(id) });
  }
}
